//index.js
//获取应用实例
const app = getApp()

Page({
 data:{
    userInfo:null,
    album_list:[],    //相册总数据容器
    arr_left:[],      //相册左右容器
    arr_right:[],
    default_bg: '',   //默认图片(在初始化时从app.js获取)
    pnow:1,
    pcount:0,
    Search_Text:'',
    search:''

 },



 //设置相册封面失效时重置为默认封面
 Load_Err(e) {
   var index = e.currentTarget.dataset.index;
   var side = e.currentTarget.dataset.side;
   var t = this
   app.SetDefaltCover(e, t);
 },


//搜索框失去焦点就保存搜索内容
 Search_Text(e){
  var Search_Text=e.detail.value;
  this.setData({
    Search_Text: Search_Text
  });
 },

 //点击搜索按钮
 Search_Button(){
    console.log(this.data.Search_Text)
    //return;

    var t = this;
    this.ResetAlbumParm(t);
    this.GetAllAlbum(t, this.data.Search_Text, function(){});
 },



  //重置相册数据
  ResetAlbumParm(t){
    t.setData({
      album_list: [],
      arr_left: [],      //相册左右容器
      arr_right: [],
      pnow: 1,
      pcount: 0,
    });
  },

 /**
  * 生命周期函数--监听页面加载
  */
 onLoad: function (options) {
   var t = this
  this.setData({
    default_bg:app.globalData.default_img
  })
  this.GetAllAlbum(t, t.data.Search_Text, function () { });
 },





 /**
  * 生命周期函数--监听页面显示
  */
 onShow: function () {
   //监听用户对相册的改动，有改动则清空缓存数据重新加载 
   var t = this;
   if (wx.getStorageSync('change')) {
      t.ResetAlbumParm(t);
      wx.setStorageSync('change', false);
      this.GetAllAlbum(t, t.data.Search_Text, function () {});
   }


 },



  GetAllAlbum(t, search){
    app.GetAllAlbum(t,'all', search,function(){})
  },





 /**
  * 页面上拉触底事件的处理函数
  */
 onReachBottom: function () {
   var t = this;

   this.GetAllAlbum(t,t.data.Search_Text,function(){});
 },


 //点击收藏
 Collection(e){
   var album_id = e.currentTarget.dataset.album_id
   var dirction = e.currentTarget.dataset.dirction
   var t = this;
   if(dirction == 'left'){
      var data = t.data.arr_left
   }else{
      var data = t.data.arr_right
   }
   app.getOpenid(function (openid) {
     app.Album_Collet(album_id, openid, function(res){
        if(res.data == 'ok'){
          wx.setStorageSync('change',true)
          //t.ResetAlbumParm(t)
          //t.GetAllAlbum(t, t.data.Search_Text, function () {});
          for (var i = 0; i < data.length; i++) {
            if (data[i].Album_Id == album_id) {
              if (data[i].isCollect) {
                data[i].isCollect = false
              } else {
                data[i].isCollect = true
              }
            }
          }
          if (dirction == 'left') {
            t.setData({
              arr_left: data
            })
          } else {
            t.setData({
              arr_right: data
            })
          }
        }
     })
   });
 },

 //点击赞
 Gooding(e){
   var album_id = e.currentTarget.dataset.album_id
   var dirction = e.currentTarget.dataset.dirction
   var t = this;   
   if (dirction == 'left') {
     var data = t.data.arr_left
   } else {
     var data = t.data.arr_right
   }
   app.getOpenid(function (openid) {
     app.SetGood(openid, album_id, function (res) {
       if (res == 'ok') {
         wx.setStorageSync('change', true)
         for (var i = 0; i < data.length; i++) {
           if (data[i].Album_Id == album_id) {
             if (data[i].isFlower) {
               data[i].isFlower = false
               data[i].Album_Flower_Count = parseInt(data[i].Album_Flower_Count) - 1
             } else {
               data[i].isFlower = true
               data[i].Album_Flower_Count = parseInt(data[i].Album_Flower_Count) + 1
             }
           }
         }
         if (dirction == 'left') {
           t.setData({
             arr_left: data
           })
         } else {
           t.setData({
             arr_right: data
           })
         }
       }
     })
   })
 },


 //点击相册时跳转到相册详情页面
 Jump_Detail(e){
    var album_id = e.currentTarget.dataset.album_id
    app.Jump_Detail(album_id);
 },

})
